/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaFinTipoEntrada.java
 *
 * Created on 22/04/2011, 16:51:38
 */
package asfabdesk.apresentacao;

import asfabdesk.dominio.DepositoFinanceiro;
import asfabdesk.dominio.TipoEntrada;
import asfabdesk.presistencia.DAO;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 *
 * @author Marco Aurelio
 */
public class TelaFinTipoEntrada extends javax.swing.JInternalFrame {

    private TipoEntrada tipoEntrada = new TipoEntrada();

    /** Creates new form TelaFinTipoEntrada */
    public TelaFinTipoEntrada() throws SQLException {
        initComponents();
        configurarComboDepositos();
    }
    
     public TelaFinTipoEntrada(TipoEntrada tipo) throws SQLException {
        initComponents();
        configurarComboDepositos();
        tipoEntrada= tipo;
        povoarCampos();
    }  

    public void salvar() {
        String msgSucessoEdt = "Registro Alterado com Sucesso!";
        String msgSucessoCad = "Tipo de Entrada Cadastrado com Sucesso!";
        String msgErroEdt = "Ops, houve um erro durante a alteração do Registro.";
        String msgErroCad = "Ops, houve um erro durante o cadastro do Tipo de Entrada.";
        String sucesso;
        String erro;
        if (verificarCampos()) {
            if (verificarDup()) {
                povoarTipoEntrada();
                if (tipoEntrada.getId() != null) {
                    sucesso = msgSucessoEdt;
                    erro = msgErroEdt;
                } else {
                    sucesso = msgSucessoCad;
                    erro = msgErroCad;
                }
                try {
                    tipoEntrada = new DAO().salvar(tipoEntrada);
                    JOptionPane.showMessageDialog(null, sucesso, "Sucesso!", 1);
                    povoarCampos();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(null, erro, "Ops,erro!", 0);
                }
            }
        }
    }

    public boolean verificarCampos() {
        boolean retorno = true;
        StringBuffer campos = new StringBuffer();
        String campo;
        if (txtDescricao.getText().equals("")) {
            campo = "(Descricao) ";
            campos.append(campo);
            retorno = false;
        }

        if (comboDeposito.getSelectedItem().toString().equals("")) {
            campo = "(Depósito Financeiro) ";
            campos.append(campo);
            retorno = false;
        }

        if (comboNatureza.getSelectedItem().toString().equals("")) {
            campo = "(Natureza)";
            campos.append(campo);
            retorno = false;
        }

        if (retorno == false) {
            JOptionPane.showMessageDialog(null, "Ops, você não preencheu o(s) seguinte(s) campo(s) obrigatório(s): " + '\n' + campos, "Campos Obrigatórios.", 2);
        }

        return retorno;
    }

    public boolean verificarDup() {
        boolean r = true;
        if (new DAO().localizar(TipoEntrada.class, "TipoEntradaPorNome", txtDescricao.getText().toUpperCase()) != null) {
            if (tipoEntrada.getDescricao() != null && tipoEntrada.getDescricao().equals(txtDescricao.getText().toUpperCase())) {
            } else {
                r = false;
            }
        }
        if (r == false) {
            JOptionPane.showMessageDialog(null, "Ops, já existe um Tipo de Entrada com este Nome!", "Ops.", 2);
        }
        return r;
    }

    public void povoarTipoEntrada() {
        try {
            tipoEntrada.setDescricao(txtDescricao.getText().toUpperCase());
            tipoEntrada.setUsuarioCadastrante(TelaLogin.usuario);
            tipoEntrada.setNatureza(comboNatureza.getSelectedIndex());
            if (comboDeposito.getSelectedItem() != null) {
                DepositoFinanceiro deposito = (new DAO().localizar(DepositoFinanceiro.class, "depositoPorNome", comboDeposito.getSelectedItem().toString()));
                tipoEntrada.setDepositoFinanceiro(deposito);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void povoarCampos(){
        txtDescricao.setText(tipoEntrada.getDescricao());
        txtUsuario.setText(tipoEntrada.getUsuarioCadastrante().getNome());
        comboNatureza.setSelectedIndex(tipoEntrada.getNatureza());
        comboDeposito.setSelectedItem(tipoEntrada.getDepositoFinanceiro().getDescricao());
        if(tipoEntrada.isStatus()){
            txtStatus.setText("Ativo");
        }else{
            txtStatus.setText("Inativo");
        }
    }

    public void configurarComboDepositos() throws SQLException {
        java.util.List depositos = (new DAO()).listar(DepositoFinanceiro.class,"depositoAptoEntrada");
        for (int i = 0; i < depositos.size(); i++) {
            String descricao = ((DepositoFinanceiro) depositos.get(i)).getDescricao();
            comboDeposito.addItem(descricao);
        }
        comboDeposito.addItem("");
        comboDeposito.setSelectedItem("");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        jSeparator4 = new javax.swing.JSeparator();
        jSeparator6 = new javax.swing.JSeparator();
        jLabel6 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtDescricao = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtUsuario = new javax.swing.JTextField();
        comboNatureza = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        comboDeposito = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txtStatus = new javax.swing.JTextField();
        jToolBar2 = new javax.swing.JToolBar();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        btnSalvar = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        btnAtivaInativa = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        btnRemover = new javax.swing.JButton();
        jSeparator5 = new javax.swing.JToolBar.Separator();
        jLabel7 = new javax.swing.JLabel();

        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        jSeparator4.setName("jSeparator4"); // NOI18N

        jSeparator6.setName("jSeparator6"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(asfabdesk.apresentacao.AsfabDeskApp.class).getContext().getResourceMap(TelaFinTipoEntrada.class);
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        setClosable(true);
        setIconifiable(true);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        txtDescricao.setName("txtDescricao"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        txtUsuario.setEditable(false);
        txtUsuario.setName("txtUsuario"); // NOI18N

        comboNatureza.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Contribuição", "Convênio", "Outros" }));
        comboNatureza.setName("comboNatureza"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        comboDeposito.setName("comboDeposito"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtDescricao, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(comboDeposito, 0, 242, Short.MAX_VALUE)
                            .addComponent(txtUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(comboNatureza, 0, 298, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(comboNatureza, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(comboDeposito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        txtStatus.setEditable(false);
        txtStatus.setName("txtStatus"); // NOI18N

        jToolBar2.setFloatable(false);
        jToolBar2.setRollover(true);
        jToolBar2.setName("jToolBar2"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N
        jToolBar2.add(jSeparator1);

        btnSalvar.setIcon(resourceMap.getIcon("btnSalvar.icon")); // NOI18N
        btnSalvar.setText(resourceMap.getString("btnSalvar.text")); // NOI18N
        btnSalvar.setToolTipText(resourceMap.getString("btnSalvar.toolTipText")); // NOI18N
        btnSalvar.setName("btnSalvar"); // NOI18N
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });
        jToolBar2.add(btnSalvar);

        jSeparator2.setName("jSeparator2"); // NOI18N
        jToolBar2.add(jSeparator2);

        btnAtivaInativa.setIcon(resourceMap.getIcon("btnAtivaInativa.icon")); // NOI18N
        btnAtivaInativa.setText(resourceMap.getString("btnAtivaInativa.text")); // NOI18N
        btnAtivaInativa.setToolTipText(resourceMap.getString("btnAtivaInativa.toolTipText")); // NOI18N
        btnAtivaInativa.setName("btnAtivaInativa"); // NOI18N
        jToolBar2.add(btnAtivaInativa);

        jSeparator3.setName("jSeparator3"); // NOI18N
        jToolBar2.add(jSeparator3);

        btnRemover.setIcon(resourceMap.getIcon("btnRemover.icon")); // NOI18N
        btnRemover.setText(resourceMap.getString("btnRemover.text")); // NOI18N
        btnRemover.setToolTipText(resourceMap.getString("btnRemover.toolTipText")); // NOI18N
        btnRemover.setName("btnRemover"); // NOI18N
        jToolBar2.add(btnRemover);

        jSeparator5.setName("jSeparator5"); // NOI18N
        jToolBar2.add(jSeparator5);

        jLabel7.setFont(resourceMap.getFont("jLabel7.font")); // NOI18N
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        salvar();
    }//GEN-LAST:event_btnSalvarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAtivaInativa;
    private javax.swing.JButton btnRemover;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JComboBox comboDeposito;
    private javax.swing.JComboBox comboNatureza;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JToolBar.Separator jSeparator5;
    private javax.swing.JSeparator jSeparator6;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JTextField txtDescricao;
    private javax.swing.JTextField txtStatus;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables
}
